﻿using System;
using System.Windows.Forms;

namespace DCS.Extensions.WindowsForms
{
    public static class FormExtensionMethods
    {   
        public static void SafeActivate(this Form form)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(form.Activate));
            }
            else
            {
                form.Activate();
            }
        }

        public static void SafeClose(this Form form)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(form.Close));
            }
            else
            {
                form.Close();
            }
        }

        public static void SafeShow(this Form form)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(form.Show));
            }
            else
            {
                form.Show();
            }
        }

        public static void SafeHide(this Form form)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(form.Hide));
            }
            else
            {
                form.Hide();
            }
        }

        public static DialogResult SafeShowDialog(this Form form)
        {
            if (form.InvokeRequired)
            {
                return (DialogResult) form.Invoke(new Func<DialogResult>(form.SafeShowDialog));
            }
            
            return form.ShowDialog();
        }

        public static void SafeEnable(this Form form)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(form.SafeEnable));
            }
            else
            {
                form.Enabled = true;
            }
        }

        public static void SafeDisable(this Form form)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action(form.SafeDisable));
            }
            else
            {
                form.Enabled = false;
            }
        }
 
        public static void SafeWindowStateSet(this Form form, FormWindowState state)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action<FormWindowState>(form.SafeWindowStateSet), state);
            }
            else
            {
                form.WindowState = state;
            }
        }

        public static void SafeHeightSet(this Form form, int height)
        {
            if (form.InvokeRequired)
            {
                form.Invoke(new Action<int>(form.SafeHeightSet), height);
            }
            else
            {
                form.Height = height;
            }
        }
    }
}